/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ufps.cuatro.util.render;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author Carlos A. Torres C. 
 */
public class CheckListManager extends MouseAdapter implements ListSelectionListener, ActionListener{ 
    private ListSelectionModel selectionModel = new DefaultListSelectionModel(); 
    private JList list = new JList(); 
    int hotspot = new JCheckBox().getPreferredSize().width; 
 
    public CheckListManager(JList list){ 
        try{
            this.list = list; 
            list.setCellRenderer(new CheckListCellRenderer(list.getCellRenderer(), selectionModel)); 
            list.registerKeyboardAction(CheckListManager.this, KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), JComponent.WHEN_FOCUSED); 
            list.addMouseListener(CheckListManager.this); 
            selectionModel.addListSelectionListener(CheckListManager.this); 
        }catch(java.lang.NullPointerException ex){}
    } 
 
    public ListSelectionModel getSelectionModel(){ 
        return selectionModel; 
    } 
 
    private void toggleSelection(int index){ 
        if(index<0) 
            return; 
 
        if(selectionModel.isSelectedIndex(index)) 
            selectionModel.removeSelectionInterval(index, index); 
        else 
            selectionModel.addSelectionInterval(index, index); 
    } 
 
    /*------------------------------[ MouseListener ]-------------------------------------*/ 
 
    @Override
    public void mouseClicked(MouseEvent me){ 
       try{
            int index = list.locationToIndex(me.getPoint()); 
            if(index<0) 
                return; 
            if(me.getX()>list.getCellBounds(index, index).x+hotspot) 
                return; 
            toggleSelection(index); 
       }catch (java.lang.NullPointerException ex){}
    } 
 
    /*-----------------------------[ ListSelectionListener ]---------------------------------*/ 
 
    public void valueChanged(ListSelectionEvent e){
        try{
            list.repaint(list.getCellBounds(e.getFirstIndex(), e.getLastIndex())); 
        }catch(java.lang.NullPointerException ex){}
    } 
 
    /*-----------------------------[ ActionListener ]------------------------------*/ 
 
    public void actionPerformed(ActionEvent e){ 
        try{
            toggleSelection(list.getSelectedIndex()); 
        }catch(java.lang.NullPointerException ex){}
    } 
    
}
